<template>
  <!-- 使用 v-for 遍历 logData 数组，动态渲染日志条目 -->
  <el-row gutter="24" style="padding: 5px 10px">
    <el-col :span="24">
      <div class="vec-rectangle-Pvt-1"></div>
      <div class="text-Fgs-2">
        <div class="p-text-Fgs-2"><span class="span-KNC-1">日志</span></div>
      </div>
      <div class="vec-rectangle-sLp-3"></div>
      <div class="vec-rectangle-Jpn-4"></div
    ></el-col>
  </el-row>
  <el-row style="max-height: 75vh; overflow-y: auto">
    <el-col :span="24" v-for="(log, index) in state.logData" :key="index">
      <div class="frame-Sob-2">
        <div class="img-MeZ-1"></div>
        <div class="vec-rectangle-jgl-2"></div>
        <div class="text-wYf-3">
          <div class="p-text-wYf-3">
            <!-- 动态显示日志时间 -->
            <span class="span-cES-1">{{ log.sendTime }}</span>
          </div>
        </div>
        <div class="text-MuZ-4">
          <div class="p-text-MuZ-4">
            <!-- 动态显示日志信息 -->
            <span class="span-jyF-1">{{ log.eventType }}</span>
          </div>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, watch, onMounted, onUnmounted } from "vue";
import { getLogPage } from "@/api/device/testlog";
const props = defineProps({
  type: {
    type: String,
    default: "ic",
  },
  testid: {
    type: String,
    default: "",
  },
  boardid: {
    type: String,
    default: "",
  },
  datasourceindex: {
    type: String,
    default: "",
  },
});

const state = reactive({
  logData: [],
  page: {
    pagenum: 1,
    pagesize: 10,
  },
  total: 0,
});
const fetchInterval = ref(null);
// 封装获取日志数据的函数
const fetchLogData = async () => {
  if (!props.boardid) {
    console.warn("boardid 为空，不发起网络请求");
    return;
  }
  try {
    getLogPage({
      ...state.page,
      type: props.type,
      datasourceindex: props.datasourceindex,
      condition: { boardid: props.boardid },
    }).then((response) => {
      state.logData = response.records || [];
    });
  } catch (error) {
    console.error("获取日志数据失败:", error);
  }
};
// 设置定时任务
const setFetchInterval = () => {
  // 5 分钟执行一次，单位为毫秒
  fetchInterval.value = setInterval(fetchLogData, 5 * 60 * 1000);
};

// 清除定时任务
const clearFetchInterval = () => {
  if (fetchInterval.value) {
    clearInterval(fetchInterval.value);
  }
};
// 监听 props 变化
// watch(
//   [() => props.type, () => props.testid, () => props.boardid],
//   () => {
//     fetchLogData();
//     clearFetchInterval();
//     setFetchInterval();
//   },
//   { immediate: true }
// );

// 组件挂载时调用获取数据函数并设置定时任务
onMounted(() => {
  fetchLogData();
  clearFetchInterval();
  setFetchInterval();
});
// 组件卸载时清除定时任务
onUnmounted(() => {
  clearFetchInterval();
});
// 定义包含 20 条日志数据的数组
</script>
<style scoped>
.text-Fgs-2 {
  display: flex;
  flex-direction: column;
  z-index: 1;
  justify-content: start;
  left: 32px;
  height: 22px;
  mix-blend-mode: normal;
  color: rgba(0, 1, 3, 1);
  font-size: 16px;
  font-weight: 500;
  opacity: 1;
  font-family: "AlibabaPuHuiTi-Medium";
  letter-spacing: 0px;
  white-space: nowrap;
}
.vec-rectangle-Pvt-1 {
  position: absolute;
  z-index: 0;
  left: 19px;

  width: 4px;
  height: 23px;
  mix-blend-mode: normal;
  background: rgba(42, 121, 250, 1);
}
.vec-rectangle-sLp-3 {
  z-index: 2;
  left: 0px;
  /* width: 320px; */
  height: 1px;
  mix-blend-mode: normal;
  background: rgba(243, 243, 244, 1);
}

.frame-Sob-2 {
  position: relative;
  min-height: 50px;
  border-radius: 2px;
  mix-blend-mode: normal;
  display: flex;
  opacity: 1;
  background: rgba(250, 250, 250, 1);
  overflow: hidden;
  box-sizing: border-box;
  border: 1px rgba(230, 230, 230, 1) solid;
  margin: 5px 0px;
}
.img-MeZ-1 {
  position: absolute;
  z-index: 0;
  border: unset !important;
  box-shadow: unset;
  left: 12px;
  top: 3px;
  width: 16px;
  height: 16px;
  mix-blend-mode: normal;
  background: url("@/assets/images/devicelog/log.png");
  background-size: 100% 100%;
}

.vec-rectangle-jgl-2 {
  position: absolute;
  z-index: 1;
  left: 12px;
  top: 25px;
  width: 256px;
  height: 1px;
  mix-blend-mode: normal;
  background: rgba(243, 243, 244, 1);
}
.text-MuZ-4 {
  position: absolute;
  display: flex;
  flex-direction: column;
  z-index: 3;
  justify-content: start;
  left: 12px;
  top: 25px;
  height: 22px;
  mix-blend-mode: normal;
  color: rgba(0, 1, 3, 0.85);
  font-size: 16px;
  font-weight: 400;
  opacity: 1;
  font-family: "AlibabaPuHuiTi-Regular";
  letter-spacing: 0px;
  white-space: nowrap;
}
.text-wYf-3 {
  position: absolute;
  display: flex;
  flex-direction: column;
  z-index: 2;
  justify-content: start;
  left: 33px;
  height: 22px;
  mix-blend-mode: normal;
  color: rgba(0, 1, 3, 0.65);
  font-size: 16px;
  font-weight: 400;
  opacity: 1;
  font-family: "AlibabaPuHuiTi-Regular";
  letter-spacing: 0px;
  white-space: nowrap;
}
</style>
